/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.market.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.eb.entity.Product;
import com.kaaction.modules.eb.service.ProductService;
import com.kaaction.modules.market.entity.Event;
import com.kaaction.modules.market.entity.Sale;
import com.kaaction.modules.market.service.EventService;
import com.kaaction.modules.market.service.SaleService;

/**
 * 促销商品Controller
 * @author zengbiao
 * @version 2017-04-19
 */
@Controller
@RequestMapping(value = "${adminPath}/market/sale")
public class SaleController extends BaseController {

	@Autowired
	private SaleService saleService;
	
	@Autowired
	private EventService eventService;
	
	@Autowired
	private ProductService productService;
	
	@ModelAttribute
	public Sale get(@RequestParam(required=false) String id) {
		Sale entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = saleService.get(id);
		}
		if (entity == null){
			entity = new Sale();
		}
		return entity;
	}
	
	@RequiresPermissions("market:sale:view")
	@RequestMapping(value = {"list", ""})
	public String list(Sale sale, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Sale> page = saleService.findPage(new Page<Sale>(request, response), sale); 
		model.addAttribute("page", page);
		return "modules/market/saleList";
	}

	@RequiresPermissions("market:sale:view")
	@RequestMapping(value = "form")
	public String form(Sale sale, Model model) {
		model.addAttribute("sale", sale);
		return "modules/market/saleForm";
	}

	@RequiresPermissions("market:sale:edit")
	@RequestMapping(value = "save")
	public String save(Sale sale, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, sale)){
			return form(sale, model);
		}
		//saleService.save(sale);
		
		//真实意图，是分2个表保存数据
		//TODO:放到事务中
		
		
		//1.good
		Product product = new Product();
		product.setType(sale.getProductType());//904,905,906
		product.setName(sale.getTitle());
		product.setIsProduct("");
		product.setCode("");
		product.setStatus("0");
		product.setInventory(0);
		product.setMarketPriceForShow(sale.getMarketPriceForShow());
		product.setSellPrice(sale.getSalePrice());
		product.setCover(sale.getCover());
		product.setIsShow("0");
		logger.info("开始日期");
		product.setRemarks(sale.getProductDescription());
		product.setEventStartDate(sale.getStartDate());
		product.setEventEndDate(sale.getEndDate());
		productService.save(product);
		//2.添加事件
		Event event = new Event();
		event.setType(sale.getProductType());//904,905,906
		event.setName(sale.getTitle());
		//event.setCoverImage(sale.getCover());
		event.setEnrollLimit(sale.getBarginLimit());
//		event.setEventStartDate(sale.getStartDate());
//		event.setEventEndDate(sale.getEndDate());
		event.setInteractStartDate(sale.getParticipateStartDate());
		event.setInteractEndDate(sale.getParticipateEndDate());
		if(sale.getBarginDurationLimit()!=null)event.setEnrollDayLimit(sale.getBarginDurationLimit());
		if(sale.getBarginFloorPrice()!=null)event.setProductFloorPrice(sale.getBarginFloorPrice());
		if(sale.getBarginFloorPriceForShow()!=null)event.setProductFloorPriceForShow(sale.getBarginFloorPriceForShow());
		event.setProduct(product);
		
		event.setRemarks(sale.getDescription());
		eventService.save(event);
		
		addMessage(redirectAttributes, "保存促销商品成功");
		return "redirect:"+Global.getAdminPath()+"/market/sale/?repage";
	}
	
	@RequiresPermissions("market:sale:edit")
	@RequestMapping(value = "delete")
	public String delete(Sale sale, RedirectAttributes redirectAttributes) {
		saleService.delete(sale);
		addMessage(redirectAttributes, "删除促销商品成功");
		return "redirect:"+Global.getAdminPath()+"/market/sale/?repage";
	}

}